package com.wsoft.hr.pm.service.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wsoft.constant.redis.HrPmRedisKeyConstant;
import com.wsoft.constant.redis.RedisKeyConstant;
import com.wsoft.core.annotation.GetRedisCache;
import com.wsoft.core.annotation.InitRedisCache;
import com.wsoft.core.exception.Asserts;
import com.wsoft.core.service.CodeRuleService;
import com.wsoft.hr.pm.constant.HrPmCodeConstant;
import com.wsoft.hr.pm.entity.HrPmActivityEntity;
import com.wsoft.hr.pm.entity.HrPmMemberTemplateEntity;
import com.wsoft.hr.pm.manager.HrPmActivityManager;
import com.wsoft.hr.pm.mapper.HrPmActivityMapper;
import com.wsoft.hr.pm.query.activity.ActivityInsertQuery;
import com.wsoft.hr.pm.query.activity.ActivityListQuery;
import com.wsoft.hr.pm.query.activity.ActivityUpdateEnableQuery;
import com.wsoft.hr.pm.query.activity.ActivityUpdateQuery;
import com.wsoft.hr.pm.service.IHrPmActivityService;
import com.wsoft.hr.pm.service.IHrPmMemberTemplateService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * <p>
 * 考核活动 服务实现类
 * </p>
 *
 * @author zhanggukai
 * @since 2025年02月27日 08:49:37
 */
@Service
public class HrPmActivityServiceImpl extends ServiceImpl<HrPmActivityMapper, HrPmActivityEntity> implements IHrPmActivityService {


    @Resource
    private CodeRuleService codeRuleService;

    @Resource
    private IHrPmMemberTemplateService memberTemplateService;

    @Resource
    private HrPmActivityManager activityManager;



    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean insert(ActivityInsertQuery query) {
        HrPmActivityEntity entity = Convert.convert(HrPmActivityEntity.class, query);
        entity.setCode(String.valueOf(codeRuleService.initCode(HrPmCodeConstant.HR_PM_ACTIVITY_CODE, null, 1)));
        entity.setStatus(0);
        activityManager.checkCycle(query.getCycleId(),query.getMemberTemplateList());
        boolean b = save(entity);
        if (b){
            //保存考核模版
          activityManager.saveBatchMemberTemplate(entity.getId(),query.getMemberTemplateList());
        }
         return b;
    }

    @Override
    @InitRedisCache(type = HrPmRedisKeyConstant.ACTIVITY_INFO,
            value = "id")
    public boolean update(ActivityUpdateQuery query) {
        HrPmActivityEntity entity = Convert.convert(HrPmActivityEntity.class, query);
        return updateById(entity);
    }

    @Override
    @InitRedisCache(type = HrPmRedisKeyConstant.ACTIVITY_INFO,
            value = "id")
    public boolean updateStatus(ActivityUpdateEnableQuery query) {
        HrPmActivityEntity entity = Convert.convert(HrPmActivityEntity.class, query);
        return updateById(entity);
    }

    @Override
    @InitRedisCache(type = HrPmRedisKeyConstant.ACTIVITY_INFO,
            value = RedisKeyConstant.REDIS_SELF)
    public boolean delete(Long id) {
        HrPmActivityEntity activity = get(id);
        if (activity.getStatus()!=0){
            Asserts.fail("[hr.pm.activity.status.error]");
        }
        //清空考核活动表
        memberTemplateService.remove(new QueryWrapper<HrPmMemberTemplateEntity>().lambda().eq(HrPmMemberTemplateEntity::getActivityId,id));
        return removeById(id);
    }

    @Override
    @GetRedisCache(type = HrPmRedisKeyConstant.ACTIVITY_INFO,
            value = RedisKeyConstant.REDIS_SELF)
    public HrPmActivityEntity get(Long id) {
        return getById(id);
    }

    @Override
    public PageInfo<HrPmActivityEntity> pageList(ActivityListQuery query) {
        PageHelper.startPage(query.getPageNum(), query.getPageSize());
        QueryWrapper<HrPmActivityEntity> queryWrapper = new QueryWrapper<>();
        if (StrUtil.isNotBlank(query.getName())){
            queryWrapper.lambda().like(HrPmActivityEntity::getName,query.getName());
        }
        if (query.getHrOrgId()!=null){
            queryWrapper.lambda().eq(HrPmActivityEntity::getHrOrgId,query.getHrOrgId());
        }
        if (query.getMemberId()!=null){
            queryWrapper.lambda().eq(HrPmActivityEntity::getUpdateBy,query.getMemberId());
        }
        if (query.getCycleId()!=null){
            queryWrapper.lambda().eq(HrPmActivityEntity::getCycleId,query.getCycleId());
        }
        if (query.getStatus()!=null){
            queryWrapper.lambda().eq(HrPmActivityEntity::getStatus,query.getStatus());
        }
        return PageInfo.of(list(queryWrapper));
    }

}
